//
//  UITabBar+badge.m
//  ShowYork
//
//  Created by hxh on 2020/11/13.
//  Copyright © 2020 hxh. All rights reserved.
//

#define TabbarItemNums 4.0
#import "UITabBar+badge.h"

@implementation UITabBar (badge)
// 显示红点
- (void)showBadgeOnItemIndex:(int)index {

    [self removeBadgeOnItemIndex:index];
    // 新建小红点
    UIView *bview = [[UIView alloc]init];
    bview.tag = 888 + index;
    bview.layer.cornerRadius = 3;
    bview.clipsToBounds = YES;
    bview.backgroundColor = RGBA(232, 85, 83, 1);
    CGRect tabFram = self.frame;
    
    float percentX = (index+0.6) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFram.size.width - 3);
   
    CGFloat y = ceilf(0.15 * tabFram.size.height);
    if (IPHONE_X) {
        y = ceilf(0.095 * tabFram.size.height);
    }
    bview.frame = CGRectMake(x, y, 6, 6);
    [self addSubview:bview];
    [self bringSubviewToFront:bview];
}

// 隐藏红点
- (void)hideBadgeOnItemIndex:(int)index {
    [self removeBadgeOnItemIndex:index];
}
// 移除控件
- (void)removeBadgeOnItemIndex:(int)index {

    for (UIView *subView in self.subviews) {
        if (subView.tag == 888 + index) {
            [subView removeFromSuperview];
        }
    }
}
@end
